﻿using System;
using System.Collections.Generic;
using System.Text;
using S7.IDAL;
using S7.MODEL;
using S7.DBUtility;
using MySql.Data.MySqlClient;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Data.Common;

namespace S7.MySqlDal
{
    public class UserInfo : IUserInfo
    {
        private MySqlParameter[] GetParames(MODEL.UserInfo info)
        {
            Type t = info.GetType();

            PropertyInfo[] propertys = t.GetProperties();

            MySqlParameter[] parames = new MySqlParameter[propertys.Length];

            int i = 0;

            foreach (PropertyInfo property in propertys)
            {
                object propertyValue = property.GetValue(info, null);
                parames[i] = new MySqlParameter("?" + property.Name, propertyValue);
                i++;
            }

            return parames;
        }

        public object RegUser(MODEL.UserInfo info)
        {
            MySqlParameter[] parames = new MySqlParameter[] { 
                new MySqlParameter("?account",info.Account),
                new MySqlParameter("?email",info.Email),
                new MySqlParameter("?name",info.Name),
                new MySqlParameter("?password",info.Password),
                new MySqlParameter("?status",info.Status),
                new MySqlParameter("?true_name",info.TrueName)
            };

            return DBHelper.ExecuteScalar(DBHelper.ConnectionString,
                CommandType.Text,
                "insert into `tx_user_info`(`account`,`password`,`name`,`true_name`,`email`,`status`) " +
                "values (?account,?password,?name,?true_name,?email,?status)" +
                ";",
                parames
                );
        }

        public bool Login(string account, string password)
        {
            MySqlParameter[] parames = new MySqlParameter[] { 
                new MySqlParameter("?account",account),
                new MySqlParameter("?password",password)
            };

            object count = DBHelper.ExecuteScalar(DBHelper.ConnectionString,
                CommandType.Text,
                "select count(`account`) " +
                "from `tx_user_info` " +
                "where `account`=?account " +
                "and `password`=?password" +
                ";",
                parames);

            int c = 0;

            if (count != null)
            {
                c = int.Parse(count.ToString());
            }

            if (c > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public MODEL.UserInfo GetInfo(string account)
        {
            MySqlParameter[] parames = new MySqlParameter[] { 
                new MySqlParameter("?account",account)
            };

            MODEL.UserInfo info = new MODEL.UserInfo();

            using (IDataReader msdr = DBHelper.ExecuteReader(DBHelper.ConnectionString,
                CommandType.Text,
                "select `account`,`password`,`name`,`true_name`,`email`,`status` " +
                "from `tx_user_info` " +
                "where `account`=?account" +
                ";",
                parames))
            {
                if (msdr != null)
                {
                    if (msdr.Read())
                    {
                        info.Account = msdr["account"] != DBNull.Value ? msdr["account"].ToString() : "-";
                        info.Email = msdr["email"] != DBNull.Value ? msdr["email"].ToString() : "-";
                        info.Name = msdr["name"] != DBNull.Value ? msdr["name"].ToString() : "-";
                        info.Password = msdr["password"] != DBNull.Value ? msdr["password"].ToString() : "-";
                        info.Status = msdr["status"] != DBNull.Value ? Convert.ToInt32(msdr["status"]) : -1;
                        info.TrueName = msdr["true_name"] != DBNull.Value ? msdr["true_name"].ToString() : "-";
                    }
                }
            }
            return info;
        }

        public int Updata(MODEL.UserInfo newInfo, string account)
        {
            MySqlParameter[] parames = new MySqlParameter[] { 
                new MySqlParameter("?account",account),
                new MySqlParameter("?email",newInfo.Email),
                new MySqlParameter("?name",newInfo.Name),
                new MySqlParameter("?password",newInfo.Password),
                new MySqlParameter("?status",newInfo.Status),
                new MySqlParameter("?true_name",newInfo.TrueName)
            };
            return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString,
                CommandType.Text,
                "update `tx_user_info` " +
                "set `password` = ?password,`name` = ?name,`true_name` = ?true_name,`email` = ?email,`status` = ?status " +
                "where `account` = ?account" +
                ";",
                parames);
        }

        public int Delete(string account)
        {
            MySqlParameter[] parames = new MySqlParameter[] { 
                new MySqlParameter("?account",account)
            };
            return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString,
                CommandType.Text,
                "delete from `tx_user_info` " +
                "where `account` = ?account" +
                ";",
                parames);
        }

        public IList<MODEL.UserInfo> GetList()
        {
            IList<MODEL.UserInfo> list = new List<MODEL.UserInfo>();

            using (IDataReader msdr = DBHelper.ExecuteReader(DBHelper.ConnectionString,
                CommandType.Text,
                "select `account`,`password`,`name`,`true_name`,`email`,`status` " +
                "from `tx_user_info`" +
                ";",
                null))
            {
                if (msdr != null)
                {
                    while (msdr.Read())
                    {
                        MODEL.UserInfo info = new MODEL.UserInfo();
                        info.Account = msdr["account"] != DBNull.Value ? msdr["account"].ToString() : "-";
                        info.Email = msdr["email"] != DBNull.Value ? msdr["email"].ToString() : "-";
                        info.Name = msdr["name"] != DBNull.Value ? msdr["name"].ToString() : "-";
                        info.Password = msdr["password"] != DBNull.Value ? msdr["password"].ToString() : "-";
                        info.Status = msdr["status"] != DBNull.Value ? Convert.ToInt32(msdr["status"]) : -1;
                        info.TrueName = msdr["true_name"] != DBNull.Value ? msdr["true_name"].ToString() : "-";
                        list.Add(info);
                    }
                }
            }
            return list;

        }

        public IList<UserFriendInfo> GetFriends(string account)
        {
            MySqlParameter[] parames = new MySqlParameter[] { 
                new MySqlParameter("?account",account)
            };
            IList<MODEL.UserFriendInfo> list = new List<MODEL.UserFriendInfo>();

            using (IDataReader msdr = DBHelper.ExecuteReader(DBHelper.ConnectionString,
                CommandType.Text,
                "select `tx_user_friends`.id,`tx_user_friends`.`friend_account` as account,`tx_user_friends`.`friend_group` as groupid,`tx_user_friend_groups`.name as groupname " +
                "from `tx_user_friends` " +
                "inner join `tx_user_friend_groups` on `tx_user_friend_groups`.`id`=`tx_user_friends`.`friend_group` " +
                "where `tx_user_friends`.`user_account`=?account" +
                ";",
                parames))
            {
                if (msdr != null)
                {
                    while (msdr.Read())
                    {
                        MODEL.UserFriendInfo info = new MODEL.UserFriendInfo();
                        info.Id = msdr["id"] != DBNull.Value ? msdr["id"].ToString() : "-";
                        info.Account = msdr["account"] != DBNull.Value ? msdr["account"].ToString() : "-";
                        info.GroupId = msdr["groupid"] != DBNull.Value ? Convert.ToInt32(msdr["groupid"]) : -1;
                        info.GroupName = msdr["groupname"] != DBNull.Value ? msdr["groupname"].ToString() : "-";
                        list.Add(info);
                    }
                }
            }
            return list;
        }
    }
}
